<?php

/** libraries
 * http://docs.joomla.org/JToolBarHelper
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla view library
jimport('joomla.application.component.view');

class bgViewbg extends JView
{
    protected $result;
    protected $pagination;
	// Overwriting JView display method
	function display($tpl = null) 
	{
	   
        $model = $this->getModel();
        $aut = new bgAuthorize();
        
       
        switch(JRequest::getVar('task')){
            case 'error':
                $this->addnew = $_POST['addnew'];
                $this->ocba_code_old = $_POST['ocba_code_old'];
                
                /* Lay lai du lieu dang them */
                $data = JRequest::getVar('jform');
                /* checkbox pool */
                /* get lis extend Facility */
                $extendFac = $model->getAllExtendFac();
                /* checkbox extend facility pool */
                $list_tbpo = '';
                foreach($extendFac as $value){
                    if(isset($_POST['cbfac'.$value->TBPO_CODE])){
                        $list_tbpo .= ",".$_POST['cbfac'.$value->TBPO_CODE];
                    }
                }
                $this->tbpo_list = substr($list_tbpo,1);
                
                /* get list disabled pool */
                $disabled_pool = $model->getBarrierFreeList();
                /* checkbox disabled pool */
                $list_disp_pool = '';
                foreach($disabled_pool as $value){
                    if(isset($_POST['cbdis'.$value->DISP_CODE])){
                        $list_disp_pool .= ",".$_POST['cbdis'.$value->DISP_CODE];
                    }
                }
                $this->list_disp_pool = substr($list_disp_pool,1);
                
                
                /* checkbox gymt */
                $disabled_gymt = $model->getGymtBarrierFreeList();
                $list_disabled_gymt = '';
                foreach($disabled_gymt as $value){
                    if(isset($_POST['cbdisgym'.$value->TBGY_CODE])){
                        $list_disabled_gymt .= ",".$_POST['cbdisgym'.$value->TBGY_CODE];
                    }
                }                
                $this->list_disabled_gymt = substr($list_disabled_gymt,1);
                /* checkbox gymt 2: equipment */
                if(isset($_POST['cbeq1'])){
                    $data['GYMT_MATF'] = 1;
                }
                if(isset($_POST['cbeq2'])){
                    $data['GYMT_MACF'] = 1;
                }
                
                
                /* checkbox dock */
                $disabled_dock = $model->getDockBarrierFreeList();
                $list_disabled_dock = '';
                foreach($disabled_dock as $value){
                    if(isset($_POST['cbdock'.$value->TBDO_CODE])){
                        $list_disabled_dock .= ",".$_POST['cbdock'.$value->TBDO_CODE];
                    }
                }                
                $this->list_disabled_dock = substr($list_disabled_dock,1);
                
                $this->data = $data;               
                
                /* get center name from id */
                $this->ocba_name = $model->centerName($this->data['OCBA_CODE']);
                $master = new bgData();
                
                /* get pool type */
                $this->poolType = $master->getPoolType();
                /* get extend fac */
                //$this->extendFac = $master->getExtendFac();
                $this->extendFac =  $extendFac;
                /* get list disabled */
                $this->disabled = $disabled_pool;
                /* get water style */
                $this->poolHolk = $master->getPoolPohk();
                /* get meeting type */
                $this->poolMetk = $master->getPoolMetk();
                
                /* get gym 1,2 */
                $this->gym = $master->getGym();
                /* get gym meeting */
                $this->gymtMetk = $master->gymtMetk();
                /* get list disabled */
                //$this->disabled = $master->getBarrierFreeList();
                $this->listDisabledGymt = $model->getGymtBarrierFreeList();
                /* get list equipment */
                $this->equipment = $master->getListEquipment();
                
                
                /* get list boat type */
                $this->boatType = $model->getBoathouseType();
                /* get list fulltime employee */
                $this->employee = $master->getListFullEmploy();
                /* get list dock type */
                $this->listDock = $master->getListDockType();
                /* get list diabled dock */
                $this->listDisabledDock = $disabled_dock;
                 
                /* get list water classification */
                $this->listWaterClass =& $model->getListWaterClass();

                 
                /* get list water classification */
                $this->listBoatEdgeCode =& $model->getListBoatEdgeCode();
                
                
                JToolBarHelper::title( '  プール・体育館・艇庫作成','sport.png' );
                JToolBarHelper::save2copy('savetemp','一時保存');
                JToolBarHelper::save('save','登録');
       	        JToolBarHelper::cancel('cancel', 'キャンセル');
                // $addNew = JRequest::getVar('addnew');
                // if($addNew == 1)
                //     $this->setLayout('a00661');
                // else 
                    $this->setLayout('a00661');
                
                break;
                
            case 'add':
                $master = new bgData();
                $this->addnew = 1;
                /* get pool type */
                $this->poolType = $master->getPoolType();
                /* get extend fac */
                //$this->extendFac = $master->getExtendFac();
                $this->extendFac =  $model->getAllExtendFac();
                /* get list disabled */
                $this->disabled = $model->getBarrierFreeList();
                /* get water style */
                $this->poolHolk = $master->getPoolPohk();
                /* get meeting type */
                $this->poolMetk = $master->getPoolMetk();
                
                /* get gym 1,2 */
                $this->gym = $master->getGym();
                /* get gym meeting */
                $this->gymtMetk = $master->gymtMetk();
                /* get list disabled */
                //$this->disabled = $master->getBarrierFreeList();
                $this->listDisabledGymt = $model->getGymtBarrierFreeList();
                /* get list equipment */
                $this->equipment = $master->getListEquipment();
                
                
                /* get list boat type */
                $this->boatType = $model->getBoathouseType();
                /* get list fulltime employee */
                $this->employee = $master->getListFullEmploy();
                /* get list dock type */
                $this->listDock = $master->getListDockType();
                /* get list diabled dock */
                $this->listDisabledDock = $model->getDockBarrierFreeList();
                /* get list DOCK_RSVF */
                $this->listDock_rsvf = $master->getListDock_rsvf();               
                 /* get list water classification */
                 $this->listWaterClass =& $model->getListWaterClass();
                 
                /* get list water classification */
                $this->listBoatEdgeCode =& $model->getListBoatEdgeCode();
                
   	            JToolBarHelper::title( '  プール・体育館・艇庫作成','sport.png' );
                JToolBarHelper::save2copy('savetemp','一時保存');
                JToolBarHelper::save('save','登録');
       	        JToolBarHelper::cancel('cancel', 'キャンセル');
                $this->setLayout('a00661');
                break;
                
            case 'edit':
                $master = new bgData();
                $this->addnew = 0;
                $cid = JRequest::getVar('cid');
                $ocba_code = is_array($cid) ? $cid[0] : $cid;
                $this->ocba_code = $ocba_code;
                /* get list pool gymt dock */
                $this->item_pool = $model->getPoolFromID($ocba_code);

                $this->item_dock = $model->getDockFromID($ocba_code);

                // var_dump($this->item_dock->DOCK_FTMF);
                // die;
                $tmp_gymt = $model->getGymtFromOCBA_ID($ocba_code);
                //check for 2nd gymt only
                if(count($tmp_gymt) == 1 && $tmp_gymt[0]->GYMT_2NDK == 2){
                    $tmp_gymt[1] = $tmp_gymt[0];
                    unset($tmp_gymt[0]);
                }
                $this->list_gymt = $tmp_gymt;
                /* get center name from id */
                $this->ocba_name = $model->centerName($ocba_code);
                
                /* get pool type */
                $this->poolType = $master->getPoolType();
                /* get extend fac */
                //$this->extendFac = $master->getExtendFac();
                $this->extendFac =  $model->getAllExtendFac();
                /* get list disabled */
                $this->disabled = $model->getBarrierFreeList();
                /* get water style */
                $this->poolHolk = $master->getPoolPohk();
                /* get meeting type */
                $this->poolMetk = $master->getPoolMetk();
                /* get list DOCK_RSVF */
                $this->listDock_rsvf = $master->getListDock_rsvf();   
                /* get gym 1,2 */
                $this->gym = $master->getGym();
                /* get gym meeting */
                $this->gymtMetk = $master->gymtMetk();
                /* get list disabled */
                //$this->disabled = $master->getBarrierFreeList();
                $this->listDisabledGymt = $model->getGymtBarrierFreeList();
                /* get list equipment */
                $this->equipment = $master->getListEquipment();
                
                
                /* get list boat type */
                $this->boatType = $model->getBoathouseType();
                /* get list fulltime employee */
                $this->employee = $master->getListFullEmploy();
                /* get list dock type */
                $this->listDock = $master->getListDockType();
                /* get list diabled dock */
                $this->listDisabledDock = $model->getDockBarrierFreeList();
                
                /* get list water classification */
                $this->listWaterClass =& $model->getListWaterClass();
                /* get list water classification */
                $this->listBoatEdgeCode =& $model->getListBoatEdgeCode();
                
                JToolBarHelper::title( 'プール・体育館・艇庫編集','sport.png' );
                JToolBarHelper::save2copy('savetemp','一時保存');
                JToolBarHelper::save('save','登録');
                JToolBarHelper::cancel('cancel', 'キャンセル');
                $this->setLayout('a00662');
            	break;
            case 'popup_center':
                
                /* set data from the model */
                //$this->listCenter = & $model->getAllCenter();
                $this->ocba_code_old = &JRequest::getVar('ocba_code_old');
                $this->listCenter = $model->getAllCenterEmpty($this->ocba_code_old);
                 
                /* set layout */         
                $this->setLayout('popup_center');
                break;
               
            default :
                /*  search  */
                $this->list['filter_search'] = &JRequest::getVar('filter_search');
                /* get pool type */
                $master = new bgData();
                $this->pooltype = $master->getPoolType();
                /* get gym type */
                $this->gymtype  = $master->getGymType();
                /* get boathouse type  */
                $this->boattype =& $model->getBoathouseType();
                /*  Get data from the model */
                
                //$this->rows =& $model->getAll($this->list);
                //Kiem tra co quyen Edit
                //if($aut->checkEditOwn()){
//                    $this->list['user_id'] = $aut->getUserID();
//                    $this->rows =& $model->getAll($this->list);
//                }else{
//                    $this->rows =& $model->getAll($this->list);
//                }
                $this->rows =& $model->getAll($this->list);
                /*var_dump($this->rows);
                die;*/
                
                $this->pagination =& $this->get('Pagination');
            
                JToolBarHelper::title('プール・体育館・艇庫管理','sport.png');
                
                if($aut->checkAdd()){
                    JToolBarHelper::addNew('add','作成');
                }
                
                if($aut->checkEdit() || $aut->checkEditOwn()){
                    JToolBarHelper::editList('edit', '編集');
                }
                
                if($aut->checkDelete()){
                    JToolBarHelper::deleteList('', 'remove','削除');
                }
                
                $this->setLayout('a00660');
		}
        
		parent::display();
	}
}




